/**
 * 全屏表单混入 必须配置table Mixin
 */
import { get, post } from '@/utils/request'
export default {
  data() {
    return {
      formMyId: '',
      formBtnDisabled: false,
      isView: false,
      changeType: false
    }
  },
  computed: {
    fullMyTitle() {
      return this.isView ? '查看' : this.isAdd ? '添加' : '修改'
    }
  },
  methods: {
    // 表单保存触发
    handleActionSave(callBack) {
      this.$refs.myForm.$refs.ruleForm.validate((valid) => {
        if (valid) {
          const formInfo = this.$refs.myForm.formInfo
          this.formInfo = formInfo
          if (!this.isAdd) {
            this.formInfo[this.markId] = this.formMyId
          }

          this.handleBeforeSaveForm()

          this.formBtnDisabled = true
          post(`${this.baseApi}${this.isAdd ? 'create' : 'edit'}`, this.formInfo)
            .then((res) => {
              if (res.code === 200) {
                this.$message.success(this.isAdd ? '添加成功' : '编辑成功')
                this.formInfo = {}

                typeof callBack === 'function' && callBack(res.data)
              }
            }).finally(() => {
              this.formBtnDisabled = false
            })
        }
      })
    },
    /**
     * 保存到草稿 进行编辑操作
     */
    handleSaveDraft() {
      this.handleActionSave((data) => {
        // 保存table刷新 防止关闭后table不刷新
        // 不推荐在表单关闭时进行每一次都重新刷新table
        this.handleGetTable()
        // 表单逻辑
        this.handleOpenMyForm(data.id)
      })
    },
    /**
     * 保存表单
     */
    handleSaveMyForm() {
      this.handleActionSave(() => {
        this.formVisible = false
        this.handleGetTable()
      })
    },
    /**
     * 触发获取表单请求
     * @param {string} id 表单id
     * @param {function} callback 回调
    */
    handleActionSelectForm(id, callBack) {
      this.formLoading = true
      get(this.findApi, { [this.markId]: id }).then((res) => {
        if (res.code === 200) {
          callBack(res.data)
        }
      }).finally(() => {
        this.formLoading = false
      })
    },
    /**
     * 获取表单数据
     * @param {string} id 表单id
     * @param {boolean} isView 是否进行查看表单
     * @param {boolean} changeType 是否为变更记录
     */
    handleOpenMyForm(id = '', isView, changeType) {
      this.$refs.myForm.$refs.ruleForm.resetFields()
      this.formVisible = true
      this.isAdd = !id
      this.formMyId = id
      if (id) {
        this.handleActionSelectForm(id, (data) => {
          const myForm = this.$refs.myForm
          myForm.formInfo = data
          this.handleTriggerSelect(myForm.formInfo)
          this.isView = !!isView
          this.changeType = changeType
        })
      } else {
        this.$refs.myForm.formInfo = {}
      }
    },
    /**
     * trigger 关联select
     */
    handleTriggerSelect() {},
    /**
     * 复制操作 复制后需要可能会涉及到关联 select 可 handleTriggerSelect
     */
    handleCopyForm(id) {
      this.$refs.myForm.$refs.ruleForm.resetFields()
      this.formVisible = true
      this.isAdd = true
      this.handleActionSelectForm(id, (data) => {
        const myForm = this.$refs.myForm
        myForm.formInfo = data
        this.$delete(myForm.formInfo, 'id')
        this.$delete(myForm.formInfo, 'code')
        this.$delete(myForm.formInfo, 'created_at')
        this.$delete(myForm.formInfo, 'sort')
        this.handleCopyFormAfter(myForm.formInfo)
        this.handleTriggerSelect(myForm.formInfo)
      })
    },
    /**
     * 保存表单前
     */
    handleBeforeSaveForm() {},
    /**
     * 复制去除
     */
    handleCopyFormAfter() {}
  }
}
